package com.liangzhong.modules.ladderMatch.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LadderEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxyconstant.ladderMatch.LadderMatchConst;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.ladderMatch.ILadderMatchView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class LadderMatchMediator extends ModuleMediator
	{
		public static const MediatorName :String = "LadderMatchMediator";
		public function LadderMatchMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():ILadderMatchView
		{
			return viewComponent as ILadderMatchView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(LadderEvent.APPLY_MATCH,onMatchHandler);
			(view as DisplayObject).addEventListener(LadderEvent.APPLY_LIST,onApplayListHandler);
			(view as DisplayObject).addEventListener(LadderEvent.ADD_FIGHT_TIME,onAddFightTimeHandler);
			(view as DisplayObject).addEventListener(LadderEvent.MORE_LADDER_RANK,onMoreLadderRankHandler);
			AppEventDispatcher.getInstance().addEventListener(LadderEvent.EXCHANGE,onExchageHandler);
			
		}
		
		/**
		 *兑换 
		 * @param event
		 * 
		 */
		protected function onExchageHandler(event:LadderEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,event.param,LadderMatchConst.EXCHANGE);
		}
		
		/**
		 *移除 
		 * @param event
		 * 
		 */
		protected function onRemoveModuleHandler(event:Event):void
		{
			//通知离开比赛二级场景
			this.sendNotification(NotificationConstants.MATCH_LEAVE_SECOND_SCENE);
			removeMediator();
		}
		
		/**
		 *更多的天梯排名  
		 * @param event
		 * 
		 */
		protected function onMoreLadderRankHandler(event:LadderEvent):void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,event.param,LadderMatchConst.MORE_LADDER_RANK);
		}
		
		/**
		 * 增加挑战次数 
		 * @param event
		 * 
		 */
		protected function onAddFightTimeHandler(event:LadderEvent):void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,event.param,LadderMatchConst.ADD_FIGHT_TIME);
		}
		/**
		 *请求报名 列表 
		 * @param event
		 * 
		 */		
		protected function onApplayListHandler(event:LadderEvent):void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,event.param,LadderMatchConst.APPLYLIST);
		}
		
		/**
		 *比赛状态 
		 * @param event
		 * 
		 */
		protected function onMatchHandler(event:LadderEvent):void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,event.param,LadderMatchConst.MATCH_STATUC);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(LadderEvent.APPLY_MATCH,onMatchHandler);
			(view as DisplayObject).removeEventListener(LadderEvent.APPLY_LIST,onApplayListHandler);
			(view as DisplayObject).removeEventListener(LadderEvent.ADD_FIGHT_TIME,onAddFightTimeHandler);
			(view as DisplayObject).removeEventListener(LadderEvent.MORE_LADDER_RANK,onMoreLadderRankHandler);
			AppEventDispatcher.getInstance().removeEventListener(LadderEvent.EXCHANGE,onExchageHandler);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [LadderMatchConst.GET_MATCH_STATUS,
				LadderMatchConst.GET_TOP_LIST,
				LadderMatchConst.GET_APPLYLIST,
			LadderMatchConst.GET_ADD_FIGHT_TIME,
			LadderMatchConst.GET_MORE_LADDER_RANK,
			LadderMatchConst.EXCHANGE,
			LadderMatchConst.LADDERMATCH_START,
			LadderMatchConst.LADDERMATCH_END,
			LadderMatchConst.GET_EXCHANGE].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case LadderMatchConst.GET_MATCH_STATUS:
					view.matchStatus(notification.getBody());
					break;
				case LadderMatchConst.GET_TOP_LIST:
					if(view != null)view.topList();
					break;
				case LadderMatchConst.GET_APPLYLIST:
					if(view != null)view.updataApplyList(notification.getBody());
					break;
				case LadderMatchConst.GET_ADD_FIGHT_TIME:
					view.addFightTime(notification.getBody());
					break;
				case LadderMatchConst.GET_MORE_LADDER_RANK:
					view.moreRank();
					break;
				case LadderMatchConst.LADDERMATCH_START://比赛开始
				{
					//mainView.unShowMainUI();
					
					view.ladderMatchStatus({status:true});
					break;
				}
				case LadderMatchConst.LADDERMATCH_END://比赛结束
				{
					//mainView.showMainUI();
					view.ladderMatchStatus({status:false});
					break;
				}
				case LadderMatchConst.GET_EXCHANGE://兑换返回
				{
					view.exchange(notification.getBody());
					break;
				}
					
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,null,LadderMatchConst.TOP_10_LIST);
			
			resName = ResourceConst.LADDERMATCH_RES;
			if(ResourceManager.instance.isContainResource(ResourceConst.LADDERMATCH_RES))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).ladderMatch[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.LADDERMATCH_RES,resList)
		}
		private function showUI():void
		{
			this.sendNotification(LadderMatchConst.LADDERMATCH_MEDIATORTOCOMMAND,{page:1},LadderMatchConst.APPLYLIST);
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.ladderMatch.LadderMatchView");
			cView.initUI();
			viewComponent = cView;
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			//通知进入比赛二级场景
			this.sendNotification(NotificationConstants.MATCH_ENTER_SECOND_SCENE);
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}